<?
include_once("./_common.php");

$apiKey = "AIzaSyBIbQfEpzHjRn9sRDNFramGCH8FM2h8UPk";

$message = $_POST['message'];

echo("
	<FORM name='form' method='post' action='$PHP_SELF'>
		<TABLE border='0' cellspacing='1'>
			<TR>
				<TD width='109' bgcolor='#5485B6'><P align='center'><FONT face='굴림' size='2' color='#CDDAE4'>message</FONT></TD>
				<TD width='541'><P>&nbsp;<INPUT type='text' name='message' SIZE=25 MAXLENGTHTH='20'></TD>
			</TR>
			<TR>
				<TD><P>&nbsp;</TD>
				<TD><P>&nbsp;<INPUT type='submit' name='submit' value='sendMessage'></TD>
			</TR>
		</TABLE>
		<input type=hidden name=mode value='up'>
	</FORM>
	<table border='0' cellspacing='1'>
	<div style='float:left;'>
		<div id='fb-root'></div>
		<script>
		(function(d, s, id) {
			var js, fjs = d.getElementsByTagName(s)[0];
			if (d.getElementById(id)) return;
			js = d.createElement(s); js.id = id;
			js.src = '//connect.facebook.net/ko_KR/all.js#xfbml=1&appId=456954247742752';
			fjs.parentNode.insertBefore(js, fjs);
		}
		(document, 'script', 'facebook-jssdk'));
		</script>

		<div class='fb-like-box' data-href='http://www.facebook.com/nadrikb' data-width='300' data-height='400' data-colorscheme='light' data-show-faces='true' data-header='true' data-stream='true' data-show-border='true' style='background-color:#fff'></div>
	</div>
	</table>");

if ($mode == 'up')
{
	$messageData = addslashes($message); 
	sendNotification($apiKey,$messageData);
}

function sendNotification($apiKey, $messageData)
{   
	$headers = array('Content-Type:application/json ; charset=UTF-8', 'Authorization:key=AIzaSyBIbQfEpzHjRn9sRDNFramGCH8FM2h8UPk');
	
	$sql = "SELECT reg_id
	          FROM gcm_table";
	
	$rst = sql_query($sql);
	
	$conidx=0;
	
	$arr   = array();
	$arr['data'] = array();
	$arr['data']['msg'] = $messageData;
	$arr['registration_ids'] = array();
	
	while($row = mysql_fetch_array($rst))
	{
		$arr['registration_ids'][$conidx] = $row['reg_id'] ;
		$conidx++;
	}
	
	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr));
	
	$response = curl_exec($ch);
	
	echo $response;
	curl_close($ch);
	//return $response;
}
?>